package com.hzp.micromall.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzp.micromall.common.core.api.CommonPage;
import com.hzp.micromall.order.dao.OmsOrderReturnReasonDao;
import com.hzp.micromall.order.entity.OmsOrderReturnReason;
import com.hzp.micromall.order.service.OmsOrderReturnReasonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author houzipeng
 * @Date 2022/2/28 22:32
 * @Version 1.0
 */
@Service
public class OmsOrderReturnReasonServiceImpl extends ServiceImpl<OmsOrderReturnReasonDao, OmsOrderReturnReason> implements OmsOrderReturnReasonService {
    @Autowired
    private OmsOrderReturnReasonDao returnReasonDao;

    @Override
    public int create(OmsOrderReturnReason returnReason) {
        returnReason.setCreateTime(new Date());
        return returnReasonDao.insert(returnReason);
    }

    @Override
    public int update(Long id, OmsOrderReturnReason returnReason) {
        returnReason.setId(id);
        return returnReasonDao.updateById(returnReason);
    }

    @Override
    public int delete(List<Long> ids) {
        LambdaQueryWrapper<OmsOrderReturnReason> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(OmsOrderReturnReason::getId, ids);
        return returnReasonDao.delete(wrapper);
    }

    @Override
    public CommonPage<OmsOrderReturnReason> getPage(Integer pageSize, Integer pageNum) {
        Page<OmsOrderReturnReason> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<OmsOrderReturnReason> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(OmsOrderReturnReason::getSort);
        returnReasonDao.selectPage(page, wrapper);
        return new CommonPage<>(page.getCurrent(), page.getTotal(), page.getSize(), page.getRecords());
    }

    @Override
    public int updateStatus(List<Long> ids, Integer status) {
        if (!status.equals(0) && !status.equals(1)) {
            return 0;
        }
        OmsOrderReturnReason record = new OmsOrderReturnReason();
        record.setStatus(status);
        LambdaQueryWrapper<OmsOrderReturnReason> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(OmsOrderReturnReason::getId, ids);
        return returnReasonDao.update(record, wrapper);
    }

    @Override
    public OmsOrderReturnReason getItem(Long id) {
        return returnReasonDao.selectById(id);
    }
}
